#include <unistd.h>     // For sleep
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>

char message[] = "Hello there!";
char buf[sizeof(message)];
const int PORT = 1024;

int main(int argc,char** argv)
{
   unsigned int value = 0xFAFBFCFD;
   unsigned int valueInit = 0xFAFBFCFD;
   unsigned int temp = value >> 8;
   temp = temp << 8;
   unsigned char mValue8_4 = value - temp;
   value = value >> 8;
   
   temp = value >> 8;
   temp = temp << 8;
   unsigned char mValue8_3 = value - temp;
   value = value >> 8;
   
   temp = value >> 8;
   temp = temp << 8;
   unsigned char mValue8_2 = value - temp;
   value = value >> 8;
   
   temp = value >> 8;
   temp = temp << 8;
   unsigned char mValue8_1 = value - temp;
   value = value >> 8;
   
   printf ("from int32 to 4*int8: value = %d val1 = %d, val2 = %d, val3 = %d, val4 = %d\n", valueInit, mValue8_1, mValue8_2, mValue8_3, mValue8_4);
    
   unsigned char int1 = 250; 
   unsigned char int2 = 251;
   unsigned char int3 = 252;
   unsigned char int4 = 253;
   unsigned int int32 = 0;
   
   int32 += int1;
   int32 = int32 << 8;
   int32 += int2;
   int32 = int32 << 8;
   int32 += int3;
   int32 = int32 << 8;
   int32 += int4; 
    
   printf ("from 4*int8 to int32: value = %d val1 = %d, val2 = %d, val3 = %d, val4 = %d\n", int32, int1, int2, int3, int4); 
    
    
    
    
    
    
    char* pMessage;
    char* pBuf;

    if (2 == argc)
    {
        if (0 != argv[1])
        {
            int len = strlen(argv[1]);
            pMessage = new char[len];
            pBuf = new char[len];
            strcpy(pMessage, argv[1]);
        }
    }
    else
    {
        int len = strlen(message);
        pMessage = new char[len];
        pBuf = new char[len];
        strcpy(pMessage, message);
    }

    int sock;
    struct sockaddr_in addr;

    sock = socket(AF_INET, SOCK_STREAM, 0);
    if(sock < 0)
    {
        perror("socket");
        return(1);
    }

    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT); // или любой другой порт...
    addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    {
        perror("connect");
        return(2);
    }

    send(sock, pMessage, strlen(pMessage), 0);
    //recv(sock, buf, sizeof(message), 0);
    
    //printf(buf);
    close(sock);

    return 0;
}